<?php

use console\db\Migration;
use common\helpers\ArrayHelper;
use common\helpers\StringHelper;
use console\models\AdminUser;

/**
 * 创建应用相关信息数据
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m200104_091355_d_app extends Migration {

    /**
     * @inheritdoc
     */
    public function safeUp() {
        $this->compact = true;
        $tablename = 'app_client';
        if ($this->isExistsTable($tablename)) {
            $this->truncateTable('{{%' . $tablename . '}}');
            $rows = $this->getData($tablename . '.php');
            if (empty($rows)) {
                $this->insert('{{%' . $tablename . '}}', [
                    'name' => date('Ymd') . StringHelper::random(2, true),
                    'secret' => Yii::$app->security->generateRandomString(),
                    'descirption' => '测试应用',
                    'status' => 10,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);
            } else {
                foreach ($rows as $row) {
                    $this->insert('{{%' . $tablename . '}}', [
                        'name' => $row['name'],
                        'secret' => $row['secret'],
                        'descirption' => $row['descirption'],
                        'status' => 10,
                        'created_at' => time(),
                        'updated_at' => time(),
                    ]);
                }
            }
        }

        $username = ArrayHelper::getValue(Yii::$app->params, 'founder.username');
        $user = AdminUser::findByUsername($username);
        $version = 'app_version';
        if ($user !== null && $this->isExistsTable($version)) {
            $this->truncateTable('{{%' . $version . '}}');
            $this->insert('{{%' . $version . '}}', [
                'admin_id' => $user->id,
                'os' => 'mp',
                'build' => 0,
                'client_id' => 1,
                'version' => '1.0.1',
                'explain' => '开发版',
                'force' => 1,
                'status' => 10,
                'created_at' => time(),
                'updated_at' => time(),
            ]);
            $this->insert('{{%' . $version . '}}', [
                'admin_id' => $user->id,
                'os' => 'mp',
                'build' => 2,
                'client_id' => 1,
                'version' => '1.0.5',
                'explain' => '正式版',
                'force' => 1,
                'status' => 10,
                'created_at' => time(),
                'updated_at' => time(),
            ]);
            $this->insert('{{%' . $version . '}}', [
                'admin_id' => $user->id,
                'os' => 'mp',
                'build' => 2,
                'client_id' => 1,
                'version' => '1.0.6',
                'explain' => '正式版',
                'force' => 1,
                'status' => 10,
                'created_at' => time(),
                'updated_at' => time(),
            ]);
        }

        //add device
        $device = 'app_device';
        if ($this->isExistsTable($device)) {
            $this->truncateTable('{{%' . $device . '}}');
            $this->insert('{{%' . $device . '}}', [
                'sn' => StringHelper::random(20),
                'imei' => md5(Yii::$app->security->generateRandomString()),
                'os' => 'android',
                'model' => 'HUAWEI P20',
                'build' => 1,
                'created_at' => time(),
                'updated_at' => time(),
            ]);
        }

        //add token
        $token = 'app_token';
        if ($this->isExistsTable($token)) {
            $this->truncateTable('{{%' . $token . '}}');
            $this->insert('{{%' . $token . '}}', [
                'client_id' => 1,
                'device_id' => 1,
                'user_id' => 1,
                'access_token' => Yii::$app->security->generateRandomString(),
                'expired_at' => time() + 15 * 86400,
                'created_at' => time(),
                'updated_at' => time(),
            ]);
        }
    }

    /**
     * @inheritdoc
     */
    public function safeDown() {
        $this->truncateTable('{{%app_token}}');
        $this->truncateTable('{{%app_device}}');
        $this->truncateTable('{{%app_version}}');
        $this->truncateTable('{{%app_client}}');
    }

}
